<?php

namespace App\Admin\Controllers;

use App\Models\PointGoods;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class PointGoodsController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title       = '积分商品';
    protected $goodsStatus = [
        'on'  => ['value' => 1, 'text' => '正常', 'color' => 'success'],
        'off' => ['value' => 2, 'text' => '修整', 'color' => 'danger'],
    ];

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new PointGoods());

        $grid->column('name', __('Name'));
        $grid->column('store.name', '门店');
        $grid->column('images_url', __('Images url'))->image('', 50);
        $grid->column('price', '所需积分');
        $grid->column('stock', __('Stock'));
        $grid->column('sell', __('Sell'));
        $grid->column('sort', __('Sort'));
        $grid->column('goods_number', __('Goods number'));
        $grid->column('status', __('Status'))->switch($this->goodsStatus);
        $grid->column('created_at', __('Created at'));
		
        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->like('name', __('Name'));
            $filter->equal('status', __('Status'))->radio(['' => '全部', 1 => '正常', 2 =>'修整']);
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(PointGoods::findOrFail($id));
        
        $show->field('name', __('Name'));
        $show->field('images_url', __('Images url'));
        $show->field('price', __('Price'));
        $show->field('stock', __('Stock'));
        $show->field('sell', __('Sell'));
        $show->field('sort', __('Sort'));
        $show->field('describe', __('Describe'));
        $show->field('goods_number', __('Goods number'));
        $show->field('status', __('Status'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        $show->field('deleted_at', __('Deleted at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new PointGoods());

        $form->hidden('store_id')->default(Admin::user()->store_id);
        $form->text('name', __('Name'))->rules('required|max:100');
        $form->image('images_url', __('Images url'))
            ->removable()
            ->uniqueName()
            ->rules('required');
        $form->currency('price', __('Price'))->symbol('￥')
            ->rules('required|min:1');
        $form->number('stock', __('Stock'))
            ->rules('required|min:1');
        $form->number('sell', __('Sell'))
            ->rules('required|min:1');
        $form->number('sort', __('Sort'))
            ->rules('required|min:1');
        $form->editor('describe', __('Describe'));
        $form->text('goods_number', __('Goods number'))
            ->rules('required|unique:goods,goods_number,{{id}}');
        $form->switch('status', __('Status'))
            ->states($this->goodsStatus);

        return $form;
    }
}
